﻿//-----------------------------------------------------------------------
// <copyright file="DiagnosticsService.cs" company="CodePlex">
//     Copyright (c) Doug Holland, CodePlex. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.Diagnostics.Services
{
    using System;

    using System.Collections;
    using System.Collections.Generic;

    using System.Data;
    using System.Data.SqlClient;

    using System.Diagnostics;

    using System.Reflection;

    using System.ServiceModel;

    using System.Xml;
    using System.Xml.Linq;

    using CodePlex.Diagnostics.Contracts;

    using CodePlex.Diagnostics.Model;

    /// <summary>
    /// Provides the default implementation of the <see cref="T:CodePlex.Diagnostics.Contracts.IDiagnosticsService"/> service contract.
    /// </summary>
    [ServiceKnownType(typeof(MethodBase))]
    public sealed class DiagnosticsService : IDiagnosticsService
    {        
        #region public Guid PublishException(ExceptionDataContract ex, Dictionary<string, string> dictionary, XElement element, IIdentityDataContract identity)

        /// <summary>
        /// Publishes the exception information contained within the specified <see cref="T:CodePlex.Diagnostics.Contracts.ExceptionDataContract"/> to the database.
        /// </summary>
        /// <param name="ex">The <see cref="T:CodePlex.Diagnostics.Contracts.ExceptionDataContract"/> containing the exception information to be published to the database.</param>
        /// <param name="dictionary">A <see cref="T:System.Collections.Generic.Dictionary`2"/> containing name value pairs associated with this exception.</param>
        /// <param name="element">A <see cref="T:System.Xml.Linq.XElement"/> representing an xml element to be associated with this exception.</param>
        /// <param name="identity">The <see cref="T:CodePlex.Diagnostics.Contracts.IIdentityDataContract"/> containing the identity information to be published to the database.</param>
        /// <returns>A <see cref="T:System.Guid"/> representing the unique publication identifier for the exception.</returns>
        public Guid PublishException(ExceptionDataContract ex, Dictionary<string, string> dictionary, XElement element, IIdentityDataContract identity)
        {
            using (EntityContext entities = new EntityContext())
            {
                ExceptionEntity exception = new ExceptionEntity(ex, identity, Guid.NewGuid());

                entities.AddToExceptionEntitySet(exception);

                entities.SaveChanges();

                return exception.PublicationId;
            }
        }

        #endregion

        #region public Guid Log(string entry, LoggingPriority priority, LoggingType type,  Dictionary<string, string> dictionary, XElement element, IIdentityDataContract identity)

        /// <summary>
        /// Writes the <see cref="T:System.String"/> using the configured logging provider.
        /// </summary>
        /// <param name="entry">The <see cref="T:System.String"/> to be logged.</param>
        /// <param name="priority">The <see cref="T:CodePlex.Diagnostics.LoggingPriority"/> of the log entry.</param>
        /// <param name="type">The <see cref="T:CodePlex.Diagnostics.LoggingType"/> of the log entry.</param>
        /// <param name="dictionary">A <see cref="T:System.Collections.Generic.Dictionary`2"/> containing name value pairs associated with this log entry.</param>
        /// <param name="element">A <see cref="T:System.Xml.Linq.XElement"/> representing an xml element to be associated with this log entry.</param>
        /// <param name="identity">The <see cref="T:CodePlex.Diagnostics.Contracts.IIdentityDataContract"/> containing the identity information to be published to the database.</param>
        /// <returns>A <see cref="T:System.Guid"/> representing the unique publication identifier for the log entry.</returns>
        public Guid Log(string entry, LoggingPriority priority, LoggingType type, Dictionary<string, string> dictionary, XElement element, IIdentityDataContract identity)
        {
            return Guid.NewGuid();
        }

        #endregion
    }
}
